package com.tc.socket.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.*;

import java.util.List;


/**
 * @Auther: tianchao
 * @Date: 2022/2/23 10:39
 * @Description:
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(new MyChannelInterceptor());
    }


    @Override
    public void configureClientOutboundChannel(ChannelRegistration registration) {
        registration.interceptors(new CustomerOutChannelInterceptor());

    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {

    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {

    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        return false;
    }


    //    @Autowired
//    @Qualifier(value = "brokerChannelExecutor")
//    @Lazy
//    private ThreadPoolTaskExecutor brokerChannelExecutor;
    /**
     * Register STOMP endpoints mapping each to a specific URL and (optionally)
     * enabling and configuring SockJS fallback options.
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //客户端连接端点
        registry.addEndpoint("/websocket")
                //.setAllowedOrigins("*")
                .setAllowedOrigins("*")
                //.setAllowedOriginPatterns("*")
                .withSockJS();
    }

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {

    }
    /**
     * java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
     */
    /**
     * Configure message broker options.
     *
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //设置支持的topic前缀,如果不符合规则，无法通讯
        //registry.enableSimpleBroker("/topic","/user");
        //默认是这个 指定用户发送的时候的默认前缀
        //registry.setUserDestinationPrefix("/user/");
        //会全局拼接 @messageMapping 如 @MessageMapping("/send")
        // 如果是以下配置 则  stompClient.send("/app/send", {}, msg);
        registry.setApplicationDestinationPrefixes("/app");
        // registry.configureBrokerChannel().taskExecutor(brokerChannelExecutor);
        //registry.enableSimpleBroker("/topic","/user","/queue");
        registry.enableStompBrokerRelay("/topic","/user","/queue")
                .setRelayHost("localhost")
                .setRelayPort(61613)
                .setClientLogin("admin")
                .setClientPasscode("admin")
        //.setVirtualHost("/")
        ;
    }
    @Bean
    @Override
    public WebSocketHandler subProtocolWebSocketHandler() {
        return new CustomerSubProtocolWebSocketHandler(clientInboundChannel(), clientOutboundChannel());
    }
}
